﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainProject.Quanlyloaihanghoa
{
    public partial class Quanlyloaihanghoa : Form
    {
        public Quanlyloaihanghoa()
        {
            InitializeComponent();
        }
        public void loadData()
        {
            QLXT_DBDataContext db = new QLXT_DBDataContext();
            //Lấy dữ liệu cho Gridview
            data_Loaihanghoa.ClearSelection();
            var kh = from p in db.LOAIHANGHOAs select p;
            data_Loaihanghoa.DataSource = kh;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Nhaploaihanghoa nhap = new Nhaploaihanghoa();
            nhap.ShowDialog(this);
            loadData();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = data_Loaihanghoa.SelectedRows;

            if (rows.Count < 1)
            {
                MessageBox.Show("Hãy chọn dòng cần chỉnh sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            DataGridViewRow r = rows[0];

            Sualoaihanghoa sua = new Sualoaihanghoa((int)r.Cells[0].Value);
            sua.ShowDialog(this);
            loadData();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = data_Loaihanghoa.SelectedRows;
            QLXT_DBDataContext db = new QLXT_DBDataContext();

            if (rows.Count < 1)
            {
                MessageBox.Show("Hãy chọn dòng cần xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            foreach (DataGridViewRow r in rows)
            {
                LOAIHANGHOA khdel = db.LOAIHANGHOAs.Single(p => p.IDLOAIHANGHOA == (int)r.Cells[0].Value);
                db.LOAIHANGHOAs.DeleteOnSubmit(khdel);
            }

            db.SubmitChanges();

            loadData();
        }
    }
}
